package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.domain.ClientTransferRecord;
import cn.wolfcode.crm.domain.DevelopFailedRecord;
import cn.wolfcode.crm.domain.LostClientRecord;
import cn.wolfcode.crm.mapper.*;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IClientService;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class ClientServiceImpl implements IClientService {


    @Autowired
    private ClientMapper clientMapper;

    @Autowired
    private ClientTransferRecordMapper transferRecord;

    @Autowired
    private DevelopFailedRecordMapper failedRecordMapper;

    @Autowired
    private LostClientRecordMapper lostRecordMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    public void saveOrUpdate(Client entity) {
        if (entity.getId() == null) {
            // 添加创建时间、创建人、没有负责人、默认是潜在客户、
            entity.setInputdate(new Date());
            entity.setInputuser(UserContext.getCurrentUser());
            entity.setState(Client.POTENTIAL);
            clientMapper.insert(entity);
        } else {
            clientMapper.updateByPrimaryKey(entity);
        }
    }

    public void delete(Long id) {
        clientMapper.deleteByPrimaryKey(id);
    }

    public Client get(Long id) {
        return clientMapper.selectByPrimaryKey(id);
    }

    public List<Client> listAll() {
        return clientMapper.selectAll();
    }

    @Override
    public PageResult query(QueryObject qo) {
        int count = clientMapper.queryCount(qo);
        if (count == 0) {
            return new PageResult(0, Collections.emptyList());
        }
        List<Client> clients = clientMapper.queryList(qo);
        return new PageResult(count, clients);
    }

    //移入客户资源池
    @Override
    public void moveToPool(Long id) {
        clientMapper.moveToPool(id);
    }

    //尝试开发，即添加负责人
    @Override
    public void exploit(Long id) {
        //添加当前负责人,如果已有负责人，则不可更新
        Client client = clientMapper.selectByPrimaryKey(id);
        if (client.getChargeuser() != null) {
            return;
        }
        client.setChargeuser(UserContext.getCurrentUser());
        clientMapper.exploit(client);
    }

    //移交客户
    @Override
    public void exchange(ClientTransferRecord record) {
        //1.添加操作时间，操作人
        record.setChangedate(new Date());
        record.setOperator(UserContext.getCurrentUser());
        //2.获取当前客户
        Client client = clientMapper.selectByPrimaryKey(record.getClient().getId());
        //3.设置新的负责人
        client.setChargeuser(record.getNewcharge());
        clientMapper.exploit(client);
        //4.保存客户移交记录
        transferRecord.insert(record);
    }

    //标记开发失败用户
    @Override
    public void developFailed(DevelopFailedRecord record) {
        //1.更改客户状态为开发失败
        Client client = clientMapper.selectByPrimaryKey(record.getClient().getId());
        client.setState(Client.DEVELOPFAILED);
        clientMapper.developFailed(client);
        //2.保存开发失败记录
        DevelopFailedRecord failedRecord = new DevelopFailedRecord();
        failedRecord.setDate(new Date());
        failedRecord.setClient(client);
        failedRecord.setCharger(client.getChargeuser());
        failedRecord.setReason(record.getReason());
        failedRecordMapper.insert(failedRecord);
    }

    //标记流失客户
    @Override
    public void lost(LostClientRecord record) {
        //1.更改客户为流失状态
        Client client = clientMapper.selectByPrimaryKey(record.getClient().getId());
        client.setState(Client.LOSTCLIENT);
        clientMapper.lost(client);
        //2.保存客户流失记录
        LostClientRecord lostClientRecord = new LostClientRecord();
        lostClientRecord.setDate(new Date());
        lostClientRecord.setClient(client);
        lostClientRecord.setCharger(client.getChargeuser());
        lostClientRecord.setReason(record.getReason());
        lostRecordMapper.insert(lostClientRecord);
    }

    //吸纳客户
    @Override
    public void absorb(Long id) {
        Client client = clientMapper.selectByPrimaryKey(id);
        client.setState(Client.POTENTIAL);//设置为潜在客户
        client.setChargeuser(UserContext.getCurrentUser());//设置当前负责人
        //更新当前用户的信息
        clientMapper.exploit(client);
    }

    public Long selectIdByName(String name) {

        return clientMapper.selectIdByName(name);
    }

    //校验客户的身份证号
    @Override
    public boolean validateIdNumber(String identitynum, Long id) {
        //根据传进来的身份证查询客户
        Client client = clientMapper.selectByIdentitynum(identitynum);
        return client == null || client.getId() == id;
    }

    @Override
    public Client selectByPrimaryKey(Long id) {
        return clientMapper.selectByPrimaryKey(id);
    }

}
